package boardgamer.yavalath.client.game;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import boardgamer.yavalath.R;
import boardgamer.yavalath.client.YavalathReplaySaver;
import boardgamer.yavalath.client.resources.StringHandler;
import com.google.common.base.Strings;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class SaveReplayDialog extends Dialog {

  private final GameActivity gameActivity;
  private final EditText fileNameTextBox;
  private final CheckBox saveCheckBox;

  public SaveReplayDialog(final GameActivity gameActivity, String message) {
    super(gameActivity);
    this.gameActivity = checkNotNull(gameActivity, "gameActivity");

    LayoutInflater inflater
        = (LayoutInflater) gameActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View saveReplayScreen = inflater.inflate(R.layout.save_replay, null /* viewRoot */);

    this.setTitle(message);
    this.fileNameTextBox = (EditText) saveReplayScreen.findViewById(R.id.saveReplayFileName);
    this.saveCheckBox = (CheckBox) saveReplayScreen.findViewById(R.id.saveReplayCheckBox);
    this.saveCheckBox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        fileNameTextBox.setEnabled(saveCheckBox.isChecked());
      }
    });

    Button quitButton = (Button) saveReplayScreen.findViewById(R.id.saveReplayQuitButton);
    quitButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        handleButtonClick(new Action() {
          @Override public void onDone() {
            gameActivity.quitGame();
          }
        });
      }
    });

    Button restartButton = (Button) saveReplayScreen.findViewById(R.id.saveReplayRestartButton);
    restartButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        handleButtonClick(new Action() {
          @Override
          public void onDone() {
            gameActivity.restartGame();
          }
        });
      }
    });

    setContentView(saveReplayScreen);
    getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  }

  private void handleButtonClick(final Action action) {
    hide();
    if (saveCheckBox.isChecked()) {
      String replayName = fileNameTextBox.getText().toString();
      
      if (Strings.isNullOrEmpty(replayName)) {
        Toast.makeText(getContext(), StringHandler.instance().saveReplayNoName, Toast.LENGTH_SHORT)
            .show();
        show();
        return;
      }
      
      final ProgressDialog loadingDialog = ProgressDialog.show(
          getContext(),
          StringHandler.instance().saveReplayLoadTitle,
          StringHandler.instance().getSaveReplayLoadText(replayName));
      gameActivity.saveReplay(replayName,
          new YavalathReplaySaver.SaveCompleteHandler() {

            @Override
            public void onComplete(String fileName) {
              loadingDialog.hide();
              action.onDone();
              Toast.makeText(
                  getContext(),
                  StringHandler.instance().getSaveReplayDoneText(fileName),
                  Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Exception exception) {
              loadingDialog.hide();
              show();
              Toast.makeText(
                  getContext(),
                  StringHandler.instance().saveReplayDoneFail,
                  Toast.LENGTH_SHORT).show();
            }
          });
    } else {
      action.onDone();
    }
  }

  private interface Action {
    void onDone();
  }
}
